use crate::solution::Solution;

impl Solution {
    pub fn merge_alternately(word1: String, word2: String) -> String {
        let mut ans: Vec<char> = vec![];
        let max_len;
        if word1.len() > word2.len() {
            max_len = word1.len();
        } else {
            max_len = word2.len();
        }

        for i in 0..max_len {
            if i < word1.len() {
                // not found in this scope ??
                res.push(word1.chars().nth(i).unwrap());
            }
            if i < word2.len() {
                // not found in this scope??
                res.push(word2.chars().nth(i).unwrap());
            }
        }

        ans.into_iter().collect()
    }

    pub fn merge_alternately2(word1: String, word2: String) -> String {
        let mut ans = String::new();
        let mut w1 = word1.chars();
        let mut w2 = word2.chars();
        let max_len = word1.len().max(word2.len());

        for i in 0..max_len {
            match (w1.next(), w2.next()) {
                (Some(c1), Some(c2)) => {
                    ans.push(c1);
                    ans.push(c2);
                },
                (Some(c1), None) => {
                    ans.push(c1);
                },
                (None, Some(c2)) => {
                    ans.push(c2);
                },
                (None, None) => {
                    break;
                }
            }
        }

        ans
    }
}
